const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp()

Page({
    data: {
        // 轮循图片
        imgs:["lunbo3.jpg","lunbo2.jpg","lunbo1.jpg","lunbo4.jpg"],
        // 轮循字幕
        noticecl:["遇你之前,他爱世界,遇你之后,他爱回家,闲装修,为爱筑家。","精彩生活，源于出色。","自由、尽情地去爱吧，你的新房有我为你包办。","走遍千山，还是故乡月圆花香，货比三家，还是闲装修划算。","闲装修，虽然素味平生，但已经做好了为你一生打算的准备。"],
        // 首页弹窗
        move_postpic: {},

        floorGoods: [],
        floorGoodss: [],
        openAttr: false,
        showChannel: 0,
        showBanner: 0,
        showBannerImg: 0,
        banner: [],
        index_banner_img: 0,
        userInfo: {},
        imgurl: '',
        sysHeight: 0,
        loading: 0,
        autoplay:true,
        showContact:1,
    },

    // -------------------------
    // 海报效果
         move_postpic() {
             this.move_postpic = wx.createAnimation({
               duration: 1000,
             })
             this.move_postpic.scale(1.1).step()
             this.setData({
               move_postpic: this.move_postpic.export()
             })
           },
           //点击+关闭海报
      gotoCaculator() {
             wx.navigateTo({
                   url: '../caculator/calculator',
              })
             setTimeout(() => {
               this.setData({
                 showpostpic: 'none'
               })
             }, 300);
           },
           //关闭海报
           cancel() {
             this.setData({
               showpostpic: 'none'
             })
           },
      //-------------------------------------------------------- 
    onPageScroll: function (e) {
        let scrollTop = e.scrollTop;
        let that = this;
        if (scrollTop >= 2000) {
            that.setData({
                showContact: 0
            })
        } else {
            that.setData({
                showContact: 1
            })
        }
    },
    onHide:function(){
        this.setData({
            autoplay:false
        })
    },
    goSearch: function () {
        wx.navigateTo({
            url: '/pages/search/search',
        })
    },
    goCategory: function (e) {
        let id = e.currentTarget.dataset.cateid;
        wx.setStorageSync('categoryId', id);
        wx.switchTab({
            url: '/pages/category/index',
        })
    },
    handleTap: function (event) {
        //阻止冒泡 
    },
    onShareAppMessage: function () {
        let info = wx.getStorageSync('userInfo');
        return {
            title: '闲装修',
            desc: '微信小程序装修商城',
            path: '/pages/index/index?id=' + info.id
        }
    },
    toDetailsTap: function () {
        wx.navigateTo({
            url: '/pages/goods-details/index',
        });
    },
    // getajsldkaj: function(){
    //   util.request(api.commondic).then(function (res) {
    //         console.log(res)
    //   })
    // },
    //查询商品分类
    getCommonDicType: function(){
      let that = this;
      util.request(api.CommonDicTypegoods).then(function (res) {
          that.setData({
            floorGoodss: res.data
        });
        console.log("hi hi hi")
          console.log(that.data.floorGoodss)
      })
    },
    getIndexData: function () {
        let that = this;
          that.setData({
              floorGoods: [],
              banner: 1,
              channel: 1,
              notice: 1,
              loading: 1,
          });
          wx.removeTabBarBadge({
             index: 2,
          })
          wx.setTabBarBadge({
              index: 2,
              text: 0
          })
        // util.request(api.IndexUrl).then(function (res) {
        //     if (res.errno === 0) {
        //         that.setData({
        //             floorGoods: res.data.categoryList,
        //             banner: res.data.banner,
        //             channel: res.data.channel,
        //             notice: res.data.notice,
        //             loading: 1,
        //         });
        //         let cartGoodsCount = '';
        //         if (res.data.cartCount == 0) {
        //             wx.removeTabBarBadge({
        //                 index: 2,
        //             })
        //         } else {
        //             cartGoodsCount = res.data.cartCount + '';
        //             wx.setTabBarBadge({
        //                 index: 2,
        //                 text: cartGoodsCount
        //             })
        //         }
        //     }
        // });
    },
    onLoad: function (options) {
        this.getChannelShowInfo();
    },
    onShow: function () {
        this.getIndexData();
        //测试
        // this.getajsldkaj();
        //调用商品分类
        this.getCommonDicType();
        var that = this;
        let userInfo = wx.getStorageSync('userInfo');
        if (userInfo != '') {
            that.setData({
                userInfo: userInfo,
            });
        };
        let info = wx.getSystemInfoSync();
        let sysHeight = info.windowHeight - 100;
        this.setData({
            sysHeight: sysHeight,
            autoplay:true
        });
        wx.removeStorageSync('categoryId');
    },
    getChannelShowInfo: function (e) {
        let that = this;
        util.request(api.ShowSettings).then(function (res) {
            if (res.errno === 0) {
                let show_channel = res.data.channel;
                let show_banner = res.data.banner;
                let show_notice = res.data.notice;
                let index_banner_img = res.data.index_banner_img;
                that.setData({
                    show_channel: show_channel,
                    show_banner: show_banner,
                    show_notice: show_notice,
                    index_banner_img: index_banner_img
                });
            }
        });
    },
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()
        this.getIndexData();
        this.getChannelShowInfo();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },

    // 小程序
    onLoad(options) {
      this.setData({
      showpostpic: 'block'
      })
      this.move_postpic()
   },
   // 海报效果
     move_postpic() {
       this.move_postpic = wx.createAnimation({
         duration: 1000,
       })
       this.move_postpic.scale(1.1).step()
       this.setData({
         move_postpic: this.move_postpic.export()
       })
     },
     //点击+关闭海报
     gotoxxx() {
       wx.navigateTo({
         url: '../xxx/xxx',
       })
       setTimeout(() => {
         this.setData({
           showpostpic: 'none'
         })
       }, 300);
     },
     //关闭海报
     cancel() {
       this.setData({
         showpostpic: 'none'
       })
     },

})